package com.sunbim.rsrv.controller.v1;


import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.util.ObjectUtil;
import com.sunbim.rsrv.common.exception.RequestException;
import com.sunbim.rsrv.params.common.Ids;
import com.sunbim.rsrv.params.protection.ProtectionParams;
import com.sunbim.rsrv.service.ProtectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文物保护工程 前端控制器
 * </p>
 *
 * @author yuhui
 * @since 2022-01-16
 */
@Api(tags = "文物")
@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/protection")
public class ProtectionController {
    @Autowired
    ProtectionService protectionService;

    @ApiOperation(value = "根据文物id查询保护记录")
    @GetMapping("selectByRelicsId")
    public List<Map<String, Object>> selectByRelicsId(@RequestParam("relics_id") Integer relicsId) {
        return protectionService.selectByRelicsId(relicsId);
    }

    @ApiOperation(value = "批量添加保护记录")
    @SaCheckRole("admin")
    @PostMapping("insertBatch")
    public String insertBatch(@RequestBody ProtectionParams params) {
        protectionService.saveBatch(params.getList());
        return "添加成功";
    }

    @ApiOperation(value = "批量更新保护记录")
    @SaCheckRole("admin")
    @PostMapping("updateBatch")
    public String updateBatch(@RequestBody ProtectionParams params) {
        protectionService.updateBatchById(params.getList());
        return "更新成功";
    }

    @ApiOperation(value = "批量删除保护记录")
    @PostMapping("deleteBatch")
    public String deleteBatch(@RequestBody Ids params) {
        if (ObjectUtil.isNull(params.getIds()) || params.getIds().size() == 0)
            throw new RequestException("删除的id列表不能为空");
        protectionService.deleteBatchById(params.getIds());
        return "删除成功";
    }
}

